#!/usr/bin/ruby
$: << File.join(File.expand_path(File.dirname(__FILE__)), "..")
require 'rubygems'
require 'optparse'
require 'EasyHadoop'


class Filter < EasyHadoop
	
	attr_reader :fields_keys, :max_index
	
	#constructor
	def initialize(arguments)
		super(arguments)
		@fields_keys = @options[:fields].keys
		@max_index = @fields_keys.sort.last
		
	end
	
	def set_mandatory
		super
		@mandatory << :fields
	end

	def set_commands
		super
		@optparse.banner = "Usage: ruby #{File.basename(__FILE__)} --field COL:FILTER --field col:FILTER [OPTIONS]"
		
		@options[:fields]=nil
		@optparse.on('-f','--field COL:FILTER,LIST', "Specify column index number on which to apply the filter functions") do |value|
			@options[:fields] = {} if @options[:fields].nil?
			col, normalizer = value.split(':')
			@options[:fields][col.to_i] = [] unless @options[:fields].has_key?(col.to_i)
			@options[:fields][col.to_i] << Regexp.new(normalizer, Regexp::IGNORECASE)
		end
	end
	
	#def execute
	def execute(line)
		
		return line if line.nil? or line.chomp.empty?
		
		tokens = line.to_s.chomp.split(@options[:input_delimiter])
		
		#Sanity Check
		if tokens.length-1 < @max_index 
			return nil if @options[:ignore]
			raise Exception, "Expecting atleast #{@max_index+1} columns. Found #{tokens.length}"
		end
		
		ignore = false
		@fields_keys.each{|f|
		  	break if ignore
			value = tokens[f]
			reg = @options[:fields][f]
			#TODO: USE REGEXP.UNION TO AVOID LOOP
			reg.each{|re|
				ignore = true if value.to_s =~ re
			}		
		}
		return nil if ignore
		return tokens.join(@options[:output_delimiter])
	end
	
	def isEmpty(value)
	  return (value.nil? or value.to_s.strip.empty?)
	end

	def isZero(value)
	  begin
	    return (value.to_f == 0.0)
	  rescue
	    return false
	  end
	end
end


# Scripting Option
if __FILE__ == $0
    cls = Filter.new(ARGV)
    STDIN.each_line{|line|
	  value = cls.execute(line)
      puts value unless value.nil?
    }
end